var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.frames["zwIframe"];
if (!upWindow) {
    upWindow = window.parent.frames["componentDiv"].frames["zwIframe"];
}
//关闭自定义扩张控件弹出窗口
function fy_dialog_close() {
    window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
}

function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}

function getUpFieldValueByName(name, index) {
    return getUpFieldValue(getUpFieldsByFieldval(name).eq(index ? index : 0));
}

function getUpFieldValue(field) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    var obj = new Object();
    obj.displayName = fieldVal.displayName;
    obj.name = fieldVal.name;
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "checkbox":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "radio":
            if (editTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            }
            break;
        case "select":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + " option:selected")
                        .attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).attr("val4cal");
            }
            break;
        case "date":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
                if (obj.value != "") {
                    obj.value = obj.value + " 00:00";
                }
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "datetime":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "flowdealoption":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "lable":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relationform":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relation":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "project":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "member":
        case "multimember":
        case "account":
        case "multiaccount":
        case "department":
        case "multidepartment":
        case "post":
        case "multipost":
        case "level":
        case "multilevel":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + "_txt").val();
                obj.id = field.find("#" + fieldVal.name).val().split('|')[1];
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).text();
            }
            break;
        case "attachment":
        case "image":
        case "document":
            if (editTag || browseTag) {
                var attNames = "";
                var tempField = field.find("#" + fieldVal.name);
                var tempFileId = "";
                if (tempField[0].tagName.toLowerCase() == "input") {
                    tempFileId = tempField.val();
                } else if (tempField[0].tagName.toLowerCase() == "span") {
                    tempFileId = tempField[0].innerHTML;
                }
                var atts = upWindow.getAttBySubreference(tempFileId);
                for (var j = 0; j < atts.length; j++) {
                    attNames += atts[j].filename + ",";
                }
                if (attNames != "") {
                    attNames = attNames.substr(0, attNames.length - 1);
                }
                obj.value = attNames;
            }
            break;
        case "outwrite":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "externalwrite-ahead":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "exchangetask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "querytask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        default:
            tag = false;
    }
    if (obj.value != undefined) {
        obj.value = obj.value.replace("\r\n", ";");
        obj.value = obj.value.replace("\n", ";");
    }
    return obj;
}

function setUpFieldValue2(field, value, displayValue, index) {

    try {

        var fieldVal = field.attr("fieldVal");
        var editTag = field.hasClass(editClass);
        var addTag = field.hasClass(addClass);
        value = (value);
        displayValue = displayValue ? (displayValue) : displayValue;
        if (fieldVal != null && typeof (fieldVal) != 'undefined') {
            fieldVal = eval("(" + fieldVal + ")");
        } else {
            return null;
        }
        var tag = true;
        switch (fieldVal.inputType) {
            case "text":
            case "textarea":
                if (editTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    inField.trigger("blur");
                } else if (addTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    inField.trigger("blur");
                }
                break;
            case "checkbox":
                if (editTag) {
                    field.find("#" + fieldVal.name).attr("checked", true);
                    field.find("#" + fieldVal.name).trigger("click");
                }
                break;
            case "radio":
                if (editTag) {
                    field.find(":radio").eq(index).attr("checked", true);
                    field.find(":radio").eq(index).trigger("click");
                }
                break;
            case "select":
                if (editTag) {
                    var options = field.find("#" + fieldVal.name + " option");
                    for (var k = 0; k < options.length; k++) {
                        if (options.eq(k).text() == value)
                            options.eq(k).attr("selected", true);
                    }
                    field.find("#" + fieldVal.name).trigger("change");
                }
                break;
            case "date":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "datetime":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "member":
            case "multimember":
            case "account":
            case "multiaccount":
            case "department":
            case "multidepartment":
            case "post":
            case "multipost":
            case "level":
            case "multilevel":
                if (editTag) {
//				alert(editTag);
                    field.find("#" + fieldVal.name).val(value);
                }
                break;
        }
    } catch (e) {
        // TODO: handle exception
    }
}
//添加或者删除行
function addOrDelRepeat(name, newLength) {
    var fields = getUpFieldsByFieldval(name);
    var oldLength = fields.length;
    var tbody = getUpFieldsByFieldval(name).eq(0).parent();
    while (!tbody.is("tbody")) {
        tbody = tbody.parent();
    }
    for (var i = 0, length = Math.abs(newLength - oldLength); i < length; i++) {
        if (newLength > oldLength) {
            upWindow.fy_addEmpty(tbody.children().last());
        } else {
            upWindow.fy_delCurrentRepeat(tbody.children().last());
        }
    }
    upWindow.fy_setReadonly();
}

//var up_doc = window.dialogArguments.document;
function $(name) {
    return document.getElementsByName(name);
}
//
//function $2(name) {
//	return window.dialogArguments.document.getElementsByName(name);
//}

var fieldNameMap = {
    address: "企业地址",
    linkman: "联系人",
    supplyCycle: "供货周期",
    supplyAddress: "发货地址",
    tel: "电话号码",
    cpurbillcode: "发票税码",
    taxRegister: "税务登记号",
    fax: "传真号码",
    couttype: ["物流方式", "物流方式PK"],
    qualitySystem: "质量体系",
    postCode: "邮编",
    companyType: ["企业类型", "企业类型PK"],
    ccuspaycond: "付款条件",
    property: "属性DF",
    email: "电子邮箱",
    ccontent: "价格条款",
    fax : "传真号码",
            cvenbank: "银行类别",
    bank: "开户银行",
    bankaccountname: "银行开户名",
    bankcode: "银行代码",
    currency: ["币种", "币种PK"],
    bankaccount: "银行账号",
    providerName: "供应商名称",
    providerCode: "供应商编码",
    qylb: '企业类别',
    zczj: '注册资金',
    clsj: '成立时间',
    zgrs: '职工总人数',
    jycs: '经营场所',
    frdb: '法人代表',
    frdbsfz: '法人代表身份证',
    nxse: '年销售额'
};
var ccontentValueArray = ["出厂价", "到库价", "FOB", "CIF", "C&F", "其他"];
var ccuspaycondValueArray = ["票后30天", "票后45天", "票后60天", "票后70天", "其他"];
function isExistInArray(array, value) {
    for (var i in array) {
        if (array[i] == value) {
            return i;
        }
    }
    return -1;
}
function setCcontentAndCcuspaycond() {
    var ccontentValue = document.getElementsByName('ccontent')[0].value;
    var ccuspaycondValue = document.getElementsByName('ccuspaycond')[0].value;
    var ccontentIndex = (ccontentValue == '' ? 0 : isExistInArray(
            ccontentValueArray, ccontentValue));
    var ccuspaycondIndex = (ccuspaycondValue == '' ? 0 : isExistInArray(
            ccuspaycondValueArray, ccuspaycondValue));

    if (ccontentValue == '') {
        document.getElementsByName('ccontent')[0].value = ccontentValueArray[0];
    }

    if (ccuspaycondValue == '') {
        document.getElementsByName('ccuspaycond')[0].value = ccuspaycondValueArray[0];
    }

    if (ccontentIndex == -1) {
        ccontentIndex = 5;
        document.getElementById("ccontentDiv").style.display = '';
    } else {
        document.getElementById("ccontentDiv").style.display = 'none';
    }

    if (ccuspaycondIndex == -1) {
        ccuspaycondIndex = 4;
        document.getElementById("ccuspaycondDiv").style.display = '';
    } else {
        document.getElementById("ccuspaycondDiv").style.display = 'none';
    }
    document.getElementsByName("ccontentSelect")[0].value = ccontentIndex;
    document.getElementsByName("ccuspaycondSelect")[0].value = ccuspaycondIndex;
}
function _ajax(url, operator, isPost) {
//    window.parent.document.getElementById("lodingDiv").style.height = window.parent.document.body.clientHeight
//            - 3 + "px";
//    window.parent.document.getElementById("lodingDiv").style.display = '';
    var xmlhttp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                text = xmlhttp.responseText;
                var data = eval("(" + text + ")");
                if (data.success)
                    operator(data);
                else
                    alert(data.data);
            } else {
                alert("连接服务器异常");
            }
//            window.parent.document.getElementById("lodingDiv").style.display = 'none';
        }
    };
    xmlhttp.open(isPost ? "post" : "get", url + "&temp=" + Math.random(), true);
    xmlhttp.send();
}

function onCcontentSelectChange(node) {
    if (node.value == 5) {
        document.getElementById("ccontentDiv").style.display = '';
        document.getElementsByName('ccontent')[0].value = '';
    } else {
        document.getElementById("ccontentDiv").style.display = 'none';
        document.getElementsByName('ccontent')[0].value = ccontentValueArray[node.value];
    }
}

function onCcuspaycondSelectChange(node) {
    if (node.value == 5) {
        document.getElementById("ccuspaycondDiv").style.display = '';
        document.getElementsByName('ccuspaycond')[0].value = '';
    } else {
        document.getElementById("ccuspaycondDiv").style.display = 'none';
        document.getElementsByName('ccuspaycond')[0].value = ccuspaycondValueArray[node.value];
    }
}

function queryProviderInfo() {
    var providerCode = $("query_providerCode")[0].value.replace(/ /g, '')
            .toUpperCase();
    $("query_providerCode")[0].value = providerCode;
//    resetAptitude();
    if (providerCode == "") {
        alert("供应商编码不能为空");
        return;
    }

    _ajax("./oa2uapController.do?method=findProviderInfoMPI&providerCode="
            + providerCode + "&corp=" + getUpFieldValueByName("单位选择器", 0).id,
            function (data) {
                var info = eval('(' + data.data + ')')
                for (var key in fieldNameMap) {
                    $(key)[0].value = info[key] ? info[key] : "";
                    if (key != "providerName" && key != "providerCode") {
                        $(key)[0].disabled = false;
                        $(key)[0].className = "manualInput";
                    }
                }
                $("ccuspaycondSelect")[0].disabled = false;
                $("ccuspaycondSelect")[0].className = "manualInput";

                $("ccontentSelect")[0].disabled = false;
                $("ccontentSelect")[0].className = "manualInput";
                setCcontentAndCcuspaycond();
//                showAptitude();
            });
}

function writeToForm() {
    for (var key in fieldNameMap) {
        try {
            if (typeof fieldNameMap[key] == 'string') {
                setUpFieldValue2(getUpFieldsByFieldval(fieldNameMap[key]).eq(0), $(key)[0].value);
            } else {
                var node = $(key)[0];
                setUpFieldValue2(getUpFieldsByFieldval(fieldNameMap[key][0]).eq(0), node.options[node.selectedIndex].text);
                setUpFieldValue2(getUpFieldsByFieldval(fieldNameMap[key][1]).eq(0), node.value);
            }
        } catch (e) {
        }
    }
    fy_dialog_close();
}

window.onload = function () {
    if (getUpFieldValueByName(("供应商编码"), 0).value != "") {
        for (var key in fieldNameMap) {
            $(key)[0].value = (typeof fieldNameMap[key] == 'string')
                    ? getUpFieldValueByName((fieldNameMap[key]), 0).value
                    : getUpFieldValueByName(fieldNameMap[key][1], 0).value;
            if (key != "providerName" && key != "providerCode") {
                $(key)[0].disabled = false;
                $(key)[0].className = "manualInput";
            }
        }
        $("ccuspaycondSelect")[0].disabled = false;
        $("ccuspaycondSelect")[0].className = "manualInput";

        $("ccontentSelect")[0].disabled = false;
        $("ccontentSelect")[0].className = "manualInput";
        setCcontentAndCcuspaycond();
    }
//    initAptitude();

}